﻿// The Nova Project by Ken Beckett.
// Copyright (C) 2007-2012 Inevitable Software, all rights reserved.
// Released under the Common Development and Distribution License, CDDL-1.0: http://opensource.org/licenses/cddl1.php

using System.Collections.Generic;

using Nova.CodeDOM;

namespace Nova.UI
{
    /// <summary>
    /// The view model for a <see cref="CodeDOM.BlockStatement"/>.
    /// </summary>
    public abstract class BlockStatementVM : StatementVM, IBlockVM
    {
        #region /* FIELDS */

        protected BlockVM _bodyVM;

        #endregion

        #region /* CONSTRUCTORS */

        /// <summary>
        /// Create a view model instance for the specified <see cref="CodeDOM.BlockStatement"/>.
        /// </summary>
        protected BlockStatementVM(BlockStatement blockStatement, bool isDescription, Dictionary<CodeObject, CodeObjectVM> dictionary)
            : base(blockStatement, dictionary)
        {
            _bodyVM = (blockStatement.Body != null && !isDescription ? new BlockVM(blockStatement.Body, this, dictionary) : null);
        }

        #endregion

        #region /* PROPERTIES */

        /// <summary>
        /// The underlying <see cref="CodeDOM.BlockStatement"/> model.
        /// </summary>
        public BlockStatement BlockStatement
        {
            get { return (BlockStatement)CodeObject; }
        }

        /// <summary>
        /// The <see cref="BlockVM"/> body.
        /// </summary>
        public BlockVM BodyVM
        {
            get { return _bodyVM; }
        }

        #endregion

        #region /* METHODS */

        #endregion

        #region /* RENDERING */

        protected override void RenderAfter(CodeRenderer renderer, RenderFlags flags)
        {
            base.RenderAfter(renderer, flags);
            if (_bodyVM != null && !flags.HasFlag(RenderFlags.Description))
                _bodyVM.Render(renderer, flags);
        }

        public override void RenderVisible(CodeRenderer renderer, RenderFlags flags)
        {
            base.RenderVisible(renderer, flags);
            if (_bodyVM != null)
                _bodyVM.RenderVisible(renderer, flags);
        }

        public override void UnRender()
        {
            if (_bodyVM != null)
                _bodyVM.UnRender();
            base.UnRender();
        }

        #endregion
    }
}
